﻿package plugin.newteam.control
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.net.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.newteam.*;
    import plugin.newteam.model.*;
    import plugin.social.consts.*;
    import pluginfw.nio.*;
    import uiresource.newteam.*;

    public class NewTewamCtrl extends NewTeamUI
    {
        private var mUIBase:NewTeam;
        private var _leader:int = 0;
        private var _state:int = 0;
        private var _isupdate:Boolean = false;
        private var _tampDic:Object;
        private var _nears:Array;
        private var currpage:int;
        private var pagecount:int;
        private var userID:int;
        private var isleader:Boolean = false;
        private var _menueData:Array;
        private var _timeID:uint = 0;
        private var noTeamArr:Array;

        public function NewTewamCtrl(param1:NewTeam)
        {
            this._nears = [];
            this._menueData = [{data:1, label:"查看信息"}, {data:2, label:"发起私聊"}, {data:3, label:"行会邀请"}, {data:4, label:"发起组队"}, {data:5, label:"申请入队"}, {data:6, label:"发起交易"}];
            this.mUIBase = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            viewClassMap["uiresource.newteam.YaoqingUI"] = YaoQingCtrl;
            loadUI("NewTeam.xml");
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            tab_anniu.selectedIndex = 0;
            tab_anniu.selectHandler = new Handler(this.clicktab);
            this.addListener();
            var _loc_1:* = Control.create(TeamControl, this);
            ModelProxy.addObserver("team", _loc_1);
            ModelProxy.change("data", "ischeck", checkbox.selected);
            this.state = 0;
            list.array = null;
            btnsview.visible = false;
            menue.visible = false;
            this.addEventListener(Event.ADDED_TO_STAGE, this.addStageHandler);
            list.renderHandler = new Handler(this.rmRender);
            zuoBtn.addEventListener(MouseEvent.CLICK, this.leftPage);
            youBtn.addEventListener(MouseEvent.CLICK, this.rightPage);
            listbg.array = new Array(5);
            return;
        }// end function

        protected function rightPage(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.currpage + 1;
            _loc_2.currpage = _loc_3;
            if (this.currpage >= this.pagecount)
            {
                this.currpage = this.pagecount;
            }
            this.setPage();
            return;
        }// end function

        protected function leftPage(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.currpage - 1;
            _loc_2.currpage = _loc_3;
            if (this.currpage <= 0)
            {
                this.currpage = 1;
            }
            this.setPage();
            return;
        }// end function

        private function setPage() : void
        {
            this.clearfilters();
            list.page = this.currpage - 1;
            zuoBtn.disabled = this.currpage <= 1;
            youBtn.disabled = this.currpage >= this.pagecount;
            return;
        }// end function

        private function clicktab(param1:int) : void
        {
            var _loc_2:Object = null;
            if (param1 == 0)
            {
                _loc_2 = this.getTeamData();
                list.array = _loc_2.myteam;
                if (list.array.length > 0)
                {
                    this.state = 1;
                }
                else
                {
                    this.state = 0;
                }
            }
            else if (param1 == 1)
            {
                list.array = this.noTeamArr;
            }
            else
            {
                list.array = this._nears;
            }
            this.pagecount = Math.ceil(list.array.length / 5);
            this.currpage = 1;
            list.page = 0;
            this.setBtn();
            this.setPage();
            return;
        }// end function

        private function clearfilters() : void
        {
            var _loc_2:UrlImage = null;
            var _loc_1:int = 0;
            while (_loc_1 < 5)
            {
                
                _loc_2 = listbg.getCell(_loc_1).getChildByName("bgimg") as UrlImage;
                _loc_2.filters = null;
                _loc_1++;
            }
            this.userID = 0;
            return;
        }// end function

        protected function addStageHandler(event:Event) : void
        {
            GlobalLayer.instance.root.addEventListener(MouseEvent.CLICK, this.stageClickHandler);
            return;
        }// end function

        protected function stageClickHandler(event:MouseEvent) : void
        {
            menue.visible = false;
            return;
        }// end function

        public function openRefresh() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this.getTeamData();
            var _loc_2:* = UserData.getUserData();
            if (_loc_1)
            {
                for each (_loc_3 in _loc_1.myteam)
                {
                    
                    if (_loc_3.rid == _loc_2.rid)
                    {
                        if (_loc_3.stageid != _loc_2.stageid)
                        {
                            PluginManager.instance.sendToServer("team", CMDteam.TEAM_INIT, null);
                        }
                    }
                }
            }
            this.getTeam();
            return;
        }// end function

        private function showlist1() : void
        {
            tab_anniu.selectedIndex = 0;
            var _loc_1:* = this.getTeamData();
            list.array = _loc_1.myteam;
            if (list.array.length > 0)
            {
                this.state = 1;
            }
            else
            {
                this.state = 0;
            }
            this.pagecount = Math.ceil(list.array.length / 5);
            this.currpage = 1;
            list.page = 0;
            this.setPage();
            return;
        }// end function

        private function rmRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:UrlImage = null;
            var _loc_8:Image = null;
            if (param2 < list.array.length)
            {
                _loc_3 = list.array[param2];
                _loc_4 = param1.getChildByName("name") as Label;
                _loc_5 = param1.getChildByName("lv") as Label;
                _loc_6 = param1.getChildByName("job") as Label;
                _loc_7 = param1.getChildByName("duizhang") as UrlImage;
                _loc_8 = param1.getChildByName("head") as Image;
                _loc_4.text = _loc_3.name;
                _loc_5.text = _loc_3.level + "级";
                _loc_6.text = UIGlobel.getCareer(_loc_3.career);
                _loc_8.x = 4;
                _loc_8.y = 5;
                _loc_8.url = Config.getUrl("assets/images/teamhead/" + _loc_3.career + "-" + _loc_3.sex + ".png", ResourceType.NORMAL);
                _loc_7.visible = _loc_3.leader == 1;
            }
            return;
        }// end function

        protected function doGm(event:Event) : void
        {
            menuelist.selectedIndex = -1;
            menue.visible = true;
            var _loc_2:* = new Point(mouseX, mouseY);
            var _loc_3:* = this.globalToLocal(_loc_2);
            menue.x = _loc_2.x;
            menue.y = _loc_2.y;
            event.stopPropagation();
            return;
        }// end function

        private function addListener() : void
        {
            closebtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            checkbox.addEventListener(MouseEvent.CLICK, this.check2Handler);
            cjdw_btn1.addEventListener(MouseEvent.CLICK, this.createHandler);
            chakan_btn.addEventListener(MouseEvent.CLICK, this.btn1Handler);
            tuichu_btn.addEventListener(MouseEvent.CLICK, this.btn2Handler);
            yichu_btn.addEventListener(MouseEvent.CLICK, this.btn3Handler);
            tisheng_btn.addEventListener(MouseEvent.CLICK, this.btn4Handler);
            shuaxin1_btn.addEventListener(MouseEvent.CLICK, this.btn10Handler);
            list.mouseHandler = new Handler(this.doSelect);
            haoyou.addEventListener(MouseEvent.CLICK, this.clickhaoyou);
            haoyou2.addEventListener(MouseEvent.CLICK, this.clickhaoyou);
            sqrd_btn.addEventListener(MouseEvent.CLICK, this.clicksq);
            yaoqingzd.addEventListener(MouseEvent.CLICK, this.clickyaoqing);
            return;
        }// end function

        protected function clickyaoqing(event:MouseEvent) : void
        {
            if (this.userID > 0)
            {
                PluginManager.instance.sendToServer("team", CMDteam.TEAM_INVITE, this.userID);
            }
            return;
        }// end function

        protected function clicksq(event:MouseEvent) : void
        {
            if (this.userID > 0)
            {
                PluginManager.instance.sendToServer("team", CMDteam.TEAM_APPLY, this.userID);
            }
            return;
        }// end function

        protected function clickhaoyou(event:MouseEvent) : void
        {
            PluginManager.instance.sendToServer("social", CMDSocial.APPLYFOR, this.userID);
            return;
        }// end function

        private function doSelect(event:Event, param2:int) : void
        {
            var _loc_3:int = 0;
            var _loc_4:UrlImage = null;
            if (param2 < 0)
            {
                return;
            }
            if (event.type == MouseEvent.CLICK)
            {
                if (list.array[param2])
                {
                    _loc_3 = 0;
                    while (_loc_3 < 5)
                    {
                        
                        _loc_4 = listbg.getCell(_loc_3).getChildByName("bgimg") as UrlImage;
                        if (param2 >= 5)
                        {
                            if (_loc_3 == param2 % 5)
                            {
                                _loc_4.filters = [new GlowFilter(16750597, 0.5, 6, 6, 2, 1, true)];
                            }
                            else
                            {
                                _loc_4.filters = null;
                            }
                        }
                        else if (_loc_3 == param2)
                        {
                            _loc_4.filters = [new GlowFilter(16750597, 0.5, 6, 6, 2, 1, true)];
                        }
                        else
                        {
                            _loc_4.filters = null;
                        }
                        _loc_3++;
                    }
                    this.userID = list.array[param2].rid;
                    if (this.isleader)
                    {
                        var _loc_5:* = list.array[param2].rid == UserData.getUserData("rid");
                        tisheng_btn.disabled = list.array[param2].rid == UserData.getUserData("rid");
                        yichu_btn.disabled = _loc_5;
                    }
                    var _loc_5:* = list.array[param2].rid == UserData.getUserData("rid");
                    chakan_btn.disabled = list.array[param2].rid == UserData.getUserData("rid");
                    haoyou.disabled = _loc_5;
                    if (btnview2.visible)
                    {
                        haoyou2.disabled = false;
                        yaoqingzd.disabled = false;
                    }
                    else if (sqrd_btn.visible)
                    {
                        sqrd_btn.disabled = false;
                    }
                }
            }
            list.selectedIndex = -1;
            return;
        }// end function

        protected function btn10Handler(event:MouseEvent) : void
        {
            this._isupdate = false;
            return;
        }// end function

        protected function btn3Handler(event:MouseEvent) : void
        {
            if (this.userID > 0)
            {
                PluginManager.instance.sendToServer("team", CMDteam.TEAM_REMOBE, this.userID);
            }
            return;
        }// end function

        protected function btn4Handler(event:MouseEvent) : void
        {
            if (this.userID > 0)
            {
                this._leader = this.userID;
                PluginManager.instance.sendToServer("team", CMDteam.TEAM_Leader, this.userID);
            }
            return;
        }// end function

        protected function btn2Handler(event:MouseEvent = null) : void
        {
            if (this._timeID > 0)
            {
                clearTimeout(this._timeID);
                this._timeID = 0;
            }
            this.state = 0;
            PluginManager.instance.sendToServer("team", CMDteam.TEAM_LEAVE, null);
            return;
        }// end function

        protected function btn1Handler(event:MouseEvent) : void
        {
            if (this.userID > 0)
            {
                Server.instance.send(114, 7, this.userID, 0, ConnectType.SYSTEM);
            }
            return;
        }// end function

        protected function createHandler(event:MouseEvent) : void
        {
            PluginManager.instance.sendToServer("team", CMDteam.TEAM_CREATE, null);
            WarningManager.show(83, ["你"]);
            return;
        }// end function

        protected function check2Handler(event:MouseEvent) : void
        {
            ModelProxy.change("data", "ischeck", checkbox.selected);
            return;
        }// end function

        protected function closeHandler(event:MouseEvent) : void
        {
            this.mUIBase.close();
            this.clearfilters();
            return;
        }// end function

        public function getTeam() : void
        {
            var _loc_5:Object = null;
            zuoBtn.disabled = true;
            youBtn.disabled = true;
            var _loc_1:* = this.getTeamData();
            var _loc_2:* = _loc_1.myteam;
            this._leader = _loc_1.leader;
            var _loc_3:* = UserData.getUserData();
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_5 = _loc_2[_loc_4];
                if (_loc_5 && this._leader == _loc_5.rid)
                {
                    _loc_5.leader = 1;
                }
                else
                {
                    _loc_5.leader = 0;
                }
                if (_loc_5.rid == _loc_3.rid)
                {
                    _loc_3.teamid = _loc_5.teamid;
                }
                _loc_4++;
            }
            this.showlist1();
            return;
        }// end function

        public function getNear(param1) : void
        {
            var _loc_3:* = undefined;
            var _loc_4:Output = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            if (this._isupdate)
            {
                return;
            }
            this._isupdate = true;
            this._tampDic = new Object();
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                if (_loc_3.rid != UserData.getUserData("rid") && _loc_3.type == 10)
                {
                    this._tampDic[_loc_3.rid] = _loc_3;
                    _loc_2.push(_loc_3.rid);
                }
            }
            if (_loc_2.length > 0)
            {
                _loc_4 = new Output();
                _loc_5 = _loc_2.length;
                _loc_4.writeShort(_loc_5);
                while (_loc_6 < _loc_5)
                {
                    
                    _loc_4.writeVarInt(_loc_2[_loc_6], false);
                    _loc_6++;
                }
                PluginManager.instance.sendToBufferServer("team", CMDteam.TEAM_COUNT, _loc_4);
            }
            else
            {
                this.noTeamArr = [];
                this._nears = [];
                this.clicktab(tab_anniu.selectedIndex);
            }
            return;
        }// end function

        public function teamBack(param1) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            this._nears.length = 0;
            this.noTeamArr = [];
            if (param1.teamInfoList)
            {
                _loc_2 = param1.teamInfoList;
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    _loc_4 = new Object();
                    _loc_4.rid = _loc_2[_loc_3].rid;
                    if (this._nears.length > 0)
                    {
                        _loc_5 = 0;
                        while (_loc_5 < this._nears.length)
                        {
                            
                            if (_loc_4.rid == this._nears[_loc_5].rid)
                            {
                                break;
                            }
                            _loc_5++;
                        }
                    }
                    _loc_4.factionName = _loc_2[_loc_3].factionName;
                    _loc_4.level = _loc_2[_loc_3].level;
                    _loc_4.name = _loc_2[_loc_3].roleName;
                    _loc_4.career = _loc_2[_loc_3].job;
                    _loc_4.teamNum = _loc_2[_loc_3].teamMemberCount;
                    _loc_4.leader = 1;
                    _loc_4.sex = _loc_2[_loc_3].sex;
                    this._nears.push(_loc_4);
                    _loc_3++;
                }
            }
            if (param1.noTeamRoleIdList)
            {
                _loc_6 = param1.noTeamRoleIdList;
                _loc_7 = 0;
                while (_loc_7 < _loc_6.length)
                {
                    
                    this.noTeamArr.push(this._tampDic[_loc_6[_loc_7]]);
                    _loc_7++;
                }
            }
            this.clicktab(tab_anniu.selectedIndex);
            return;
        }// end function

        public function get state() : int
        {
            return this._state;
        }// end function

        public function set state(param1:int) : void
        {
            this._state = param1;
            this.setBtn();
            return;
        }// end function

        private function setBtn() : void
        {
            switch(tab_anniu.selectedIndex)
            {
                case 0:
                {
                    shuaxin1_btn.visible = false;
                    if (this.state == 0)
                    {
                        btnsview.visible = false;
                        cjdw_btn1.visible = true;
                        tuichu_btn.visible = false;
                        sqrd_btn.visible = false;
                        btnview1.visible = true;
                        sqrd_btn.visible = false;
                    }
                    else
                    {
                        btnsview.visible = true;
                        cjdw_btn1.visible = false;
                        tuichu_btn.visible = true;
                        btnview1.visible = false;
                        this.updateleader();
                    }
                    btnview2.visible = false;
                    break;
                }
                case 1:
                {
                    btnview2.visible = true;
                    haoyou2.disabled = true;
                    yaoqingzd.disabled = true;
                    btnsview.visible = false;
                    btnview1.visible = false;
                    shuaxin1_btn.visible = true;
                    break;
                }
                case 2:
                {
                    btnview1.visible = true;
                    btnsview.visible = false;
                    btnview2.visible = false;
                    shuaxin1_btn.visible = true;
                    if (this.state == 0)
                    {
                        sqrd_btn.visible = true;
                        sqrd_btn.disabled = true;
                    }
                    else
                    {
                        sqrd_btn.visible = false;
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function getTeamData() : Object
        {
            var _loc_1:* = ModelProxy.getValue("data", "troles");
            if (_loc_1 == null)
            {
                _loc_1 = new Object();
                _loc_1.myteam = [];
                ModelProxy.change("data", "troles", _loc_1);
            }
            return _loc_1;
        }// end function

        public function updateleader() : void
        {
            var _loc_3:Object = null;
            yichu_btn.disabled = true;
            tisheng_btn.disabled = true;
            this.isleader = false;
            var _loc_1:* = this.getTeamData();
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.myteam.length)
            {
                
                _loc_3 = _loc_1.myteam[_loc_2];
                if (_loc_3.leader == 1 && _loc_3.rid == UserData.getUserData("rid"))
                {
                    yichu_btn.disabled = false;
                    tisheng_btn.disabled = false;
                    this.isleader = true;
                    break;
                }
                _loc_2++;
            }
            return;
        }// end function

        public function autoleave() : void
        {
            var _loc_1:* = UserData.getUserData();
            if (_loc_1.teamid == 0)
            {
                return;
            }
            this._timeID = setTimeout(this.btn2Handler, 1000);
            return;
        }// end function

    }
}
